from pathlib import Path
filetypes = ('.tmp', '.log', '.obj', '.txt')     #指定要删除的文件类型
def delCertainFiles(directory):
    p = Path(directory)
    if not p or not p.is_dir():
        return
    for temp in p.rglob("*"):
        file = Path(temp)
        size = file.stat().st_size
        if file.is_file() and (file.suffix in filetypes or size==0):
            file.chmod(0o777)                   #修改文件属性，获取删除权限
            file.unlink()                        #删除文件
            print(file, ' deleted....')
delCertainFiles(r'C:\test')